from flask import Blueprint, render_template, g, redirect, request, url_for
from app import db
from app.models import Product, User, Updatejour, Group
import time

database_server_page = Blueprint('database_server_page', __name__, template_folder='templates')


@database_server_page.route('/database-server', methods=['GET', 'POST'])
def database_server():
    if g.user is not None and g.user.is_authenticated:
        server_lists = db.session.query(Product.server_name, Product.server_type, Product.server_address,
                                        Product.server_port, Product.creat_date, Product.creat_person, Product.id,
                                        Product.edit_date, Product.edit_person).filter().all()
        pro_num = len(server_lists)
        hs_current_id = str(g.user).replace("'", "")
        hs_current_user = db.session.query(User.username).filter_by(id=hs_current_id).first()[0]
        group_lists = db.session.query(Group.small_group).filter().all() or [['测试demo数据1', ''], ['测试demo数据2', ''],
                                                                             ['测试demo数据3', '']]
        mes_lists = db.session.query(Updatejour.output, Updatejour.update_date, Updatejour.update_time,
                                     Updatejour.message, Updatejour.id, Updatejour.log_path).filter_by(read_status=1,
                                                                                                       up_persion=hs_current_user).all()
        mes_len = len(mes_lists)
        if request.method == 'POST':

            if request.values.get('fun') == 'deleteform':
                get_num = request.values.get('id_num')
                to_delete = Product.query.filter_by(id=int(get_num)).first()
                db.session.delete(to_delete)
                db.session.commit()
            elif request.values.get('fun') == 'deletemes':
                de_id = request.values.get('mes_id')
                u = Updatejour.query.filter_by(id=de_id).first()
                u.read_status = 2
                db.session.add(u)
                db.session.commit()
            else:
                hs_name = request.values.get('serverName')
                hs_address = request.values.get('serverAddress')
                hs_port = request.values.get('serverPort')
                hs_type = request.values.get('serverType')
                hs_user = request.values.get('serverUser')
                hs_password = request.values.get('serverPassword')
                hs_remark = request.values.get('remark')
                hs_config = request.values.get('server_config')
                hs_group = request.values.get('group')
                hs_vmware = request.values.get('vmwareName')
                hs_current_date = str(time.strftime('%Y.%m.%d', time.localtime(time.time())))
                hs_creat_person = hs_current_user

                hs_obj = Product(server_name=hs_name, server_address=hs_address, server_port=hs_port,
                                 server_type=hs_type,
                                 server_user=hs_user, server_password=hs_password, creat_date=hs_current_date,
                                 creat_person=hs_creat_person, edit_date=hs_current_date, group=hs_group,
                                 server_config=hs_config, remark=hs_remark, vmware_name=hs_vmware, members=' ',
                                 edit_person=hs_current_user)
                db.session.add(hs_obj)
                db.session.commit()
                return redirect('/database-server')

    else:
        return redirect('/login')
    return render_template('hs-database-server.html', server_lists=server_lists, pro_num=pro_num,
                           hs_current_user=hs_current_user, mes_lists=mes_lists, mes_len=mes_len,
                           group_lists=group_lists)
